package com.house.core.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.house.core.dao.EmployeesDao;
import com.house.core.db.DBManager;
import com.house.core.db.OpQueryList;
import com.house.core.db.OpQueryUniq;
import com.house.core.db.OpUpdate;
import com.house.core.model.Employees;
import com.house.core.model.Employees.Role;
import com.house.core.util.MD5Util;

/**
 *
 *
 * @author <a href="mailto:chong.sun@renren-inc.com">sun chong</a>
 * @version 2013-2-21
 */
public class EmployeesDaoImpl implements EmployeesDao{
	private DBManager dbManager;
	public EmployeesDaoImpl(DBManager dbManager){
		this.dbManager = dbManager;
	}
	
	@Override
	public Employees getEmployeesByUid(final long id) {
		String sql = "select * from employees where id = ?";
		OpQueryUniq op = new OpQueryUniq(sql){
			@Override
			public void setParam(PreparedStatement ps) throws SQLException {
				ps.setLong(1, id);
			}
			@Override
			public Object parse(ResultSet rs) throws SQLException {
				return parseEmployees(rs);
			}
		};
		try {
			return (Employees) dbManager.queryUniq(op);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
	private Employees parseEmployees(ResultSet rs) throws SQLException{
		Employees employees = new Employees();
		employees.setId(rs.getLong("id"));
		employees.setUsername(rs.getString("username"));
		employees.setPassword(rs.getString("password"));
		employees.setNickname(rs.getString("nickname"));
		employees.setCardno(rs.getString("cardno"));
		employees.setAddress(rs.getString("address"));
		employees.setRole(Role.valueOf(rs.getInt("role")));
		employees.setAuthority(rs.getLong("authority"));
		employees.setJoiningTraderId(rs.getInt("joining_trader_id"));
		return employees;
	}

	@Override
	public Employees getEmployeesByUsername(final String username) {
		String sql = "select * from employees where username = ?";
		OpQueryUniq op = new OpQueryUniq(sql){
			@Override
			public void setParam(PreparedStatement ps) throws SQLException {
				ps.setString(1, username);
			}
			@Override
			public Object parse(ResultSet rs) throws SQLException {
				return parseEmployees(rs);
			}
		};
		try {
			return (Employees) dbManager.queryUniq(op);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public long addEmployees(final Employees employees) {
		String sql = "insert into employees(username, password, nickname, cardno, address, role, authority, joining_trader_id) values(?, ?, ?, ?, ?, ?, ?, ?)";
		OpUpdate op = new OpUpdate(sql){
			@Override
			public void setParam(PreparedStatement ps) throws SQLException {
				int index = 1;
				ps.setString(index++, employees.getUsername());
				ps.setString(index++, MD5Util.md5(employees.getPassword()));
				ps.setString(index++, employees.getNickname());
				ps.setString(index++, employees.getCardno());
				ps.setString(index++, employees.getAddress());
				ps.setInt(index++, employees.getRole().getValue());
				ps.setLong(index++, employees.getAuthority());
				ps.setInt(index++, employees.getJoiningTraderId());
			}
		};
		try {
			return dbManager.insertReturnLongId(op);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Employees> getEmployeesLikeUsername(String username) {
		String sql = "select * from employees where username like '" + username + "%'";
		OpQueryList op = new OpQueryList(sql){
			@Override
			public Object parse(ResultSet rs) throws SQLException {
				return parseEmployees(rs);
			}
		};
		try {
			return (List<Employees>) dbManager.queryList(op);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}
